package yns.springframework.mvc.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import yns.springframework.mvc.bean.User;

@Controller
@RequestMapping(value = "/guide")
public class GuideController {

	/**
	 * SpringMVC 基本使用
	 * 
	 * 
	 * @return
	 */
	@RequestMapping(value = "/guideMethod")
	public ModelAndView guideMethod() {
		System.out.println("guideMethod");
		ModelAndView mv = new ModelAndView();

		mv.addObject("message", "<a>Hello World!</a>");

		mv.setViewName("test");

		return mv;
	}

	/**
	 * SpringMVC 返回json数据，指定name变量非必须
	 * 
	 * 
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/guideMethod2", produces = { MediaType.APPLICATION_JSON_VALUE })
	@ResponseBody
	public String guideMethod2(@RequestParam(value = "name", required = false) String name) {
		System.out.println("guideMethod2");

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("info", "中文");
		jsonObject.put("key2", new JSONArray());
		jsonObject.put("html", "<p>Hello World!</p>");
		jsonObject.put("name", name);

		return jsonObject.toString();

	}

	/**
	 * SpringMVC 返回json数据，映射实体User,没有的属性会使用构造函数的值初始化
	 * 
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/guideMethod3", produces = { MediaType.APPLICATION_JSON_VALUE })
	@ResponseBody
	public String guideMethod3(@ModelAttribute("user") User user) {
		System.out.println("guideMethod3");

		System.out.println(user.getName());
		System.out.println(user.getAge());

		List<User> list = new ArrayList<User>();
		for (int i = 0; i < 3; i++) {
			User listUser = new User();
			listUser.setName("这是中文" + i);
			listUser.setAge(i);
			list.add(listUser);
		}

		System.out.println(JSONObject.toJSONString(user));
		System.out.println(JSONArray.toJSONString(list));

		return JSONArray.toJSONString(list).toString();

	}

	/**
	 * SpringMVC rest风格URL，获取rest URL中的参数
	 * 
	 * 
	 * @param msg
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/guideMethod4/{msg}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@ResponseBody
	public String guideMethod4(@PathVariable(value = "msg") String msg, User user) {
		System.out.println("guideMethod4");
		System.out.println(msg);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("key", user.getName());
		jsonObject.put("key2", msg);

		return jsonObject.toString();

	}

	/**
	 * SpringMVC 接受含指定变量的访问
	 * 
	 * 
	 * @param msg
	 * @param user
	 * @param submitFlag
	 * @return
	 */
	@RequestMapping(value = "/guideMethod5", params = "submitFlag=create", produces = { MediaType.APPLICATION_JSON_VALUE })
	@ResponseBody
	public String guideMethod5(String msg, User user, String submitFlag) {
		System.out.println("guideMethod5");

		System.out.println(submitFlag);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("key", user.getName());
		jsonObject.put("key2", msg);

		return jsonObject.toString();

	}

	/**
	 * SpringMVC 多个URL映射同一个方法(特别的，多与一级静态资源的相对路径需要对应多一级)
	 * test6 对应                              js/jslib/jquery/jquery-1.11.1.min.js 
	 * test6/test 对应   ../js/jslib/jquery/jquery-1.11.1.min.js
	 * 
	 * 
	 * 
	 * @return
	 */
	@RequestMapping(value = { "/guideMethod6", "/guideMethod6/guideMethod" })
	public ModelAndView guideMethod6() {
		System.out.println("guideMethod6");
		
		ModelAndView mv = new ModelAndView();
		mv.addObject("message", "Hello World!");
		mv.setViewName("test");
		
		
		return mv;
	}

	/**
	 * SpringMVC 只接受post请求
	 * 
	 * 
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/guideMethod7", produces = { MediaType.APPLICATION_JSON_VALUE }, method = RequestMethod.POST)
	@ResponseBody
	public String guideMethod7(@RequestParam(value = "name", required = false) String name) {
		System.out.println("guideMethod7");

		System.out.println(name);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("key", "value");
		jsonObject.put("key2", new JSONArray());

		return jsonObject.toString();

	}
	
	
	/**
	 * 指定请求变量非必须,如果没有设置defaultValue则对应变量返回null
	 * 
	 * 
	 * @param name
	 * @param age
	 * @return
	 */
	@RequestMapping(value = "/guideMethod8")
	@ResponseBody
	public String guideMethod8(
			@RequestParam(value = "name", required = false, defaultValue = "Default") String name,
			@RequestParam(value = "age", required = false) String age) {
		
		System.out.println("guideMethod8");
		System.out.println(name);
		System.out.println(age);
		
		if (name == null) {
			name = "Default value for exam3 : name";
		}
		if (age == null) {
			age = "Default value for exam3 : age";
		}
		
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("name", name);
		jsonObject.put("age", age);
		
		return jsonObject.toJSONString();
	}
}
